﻿#include "UserDetail_toModifyPasswordDlg.h"



UserDetail_toModifyPasswordDlg::UserDetail_toModifyPasswordDlg(QWidget* parent)
	:QWidget(parent)
{
	this->setWindowTitle("修改用户密码");
	this->resize(300, 200);		
	this->setWindowFlag(Qt::FramelessWindowHint);						//去掉边框和状态栏；
	QObject::installEventFilter(new eventFilterObject(this));			//自由拖动窗口
	this->setStyleSheet(R"(background-color:rgb(40,40,40);
									color:white;)");
	//this->setWindowFlag(Qt::WindowStaysOnTopHint);						//设置窗口置顶

	init();
}

UserDetail_toModifyPasswordDlg::~UserDetail_toModifyPasswordDlg()
{

}

void UserDetail_toModifyPasswordDlg::init()
{
	m_user_id_edit = new QLineEdit;
	m_user_id_edit->setStyleSheet("border:none;background-color:white;color:black;");
	m_username_edit = new QLineEdit;
	m_username_edit->setStyleSheet("border:none;background-color:white;color:black;");
	m_oldpassword_edit = new QLineEdit;
	m_oldpassword_edit->setStyleSheet("background-color:white;color:black");
	m_newpassword_edit = new QLineEdit;
	m_newpassword_edit->setStyleSheet("background-color:white;color:black");
	m_ok_btn = new QPushButton("ok");
	m_cancel_btn = new QPushButton("cancel");


	//总体布局：
	totality_layout = new QVBoxLayout(this);
	totality_layout->setSizeConstraint(QLayout::SetFixedSize);			//将布局的大小约束设置为固定大小，也就是布局的大小将被固定为一个固定的宽度和高度

	totality_layout->addWidget(new QLabel("Change Password Window >"));
	totality_layout->addSpacerItem(new QSpacerItem(1, 30));				//宽度为1，高度为30像素；


	//输入内容部分布局：
	auto form_layout = new QFormLayout;

	form_layout->addRow("<font color=red>*</font>用户id >", m_user_id_edit);
	form_layout->addRow(new QLabel("<font size=2 face='微软雅黑' color='gray'>此窗口该用户的id不可修改</font>"));
	form_layout->addRow("<font color=red>*</font>用户名 >", m_username_edit);
	form_layout->addRow(new QLabel("<font size=2 face='微软雅黑' color='gray'>此窗口该用户的用户名 不可修改</font>"));
	form_layout->addRow("<font color=red>*</font>用户密码 >", m_oldpassword_edit);
	form_layout->addRow(new QLabel("<font size=2 face='微软雅黑' color='gray'>原密码</font>"));
	//把输入内容部分布局添加到总体布局：
	totality_layout->addLayout(form_layout);

	{
		newpassword_layout = new QFormLayout;

		newpassword_layout->addRow("<font color=red>*</font>修改用户密码 >", m_newpassword_edit);
		newpassword_layout->addRow(new QLabel("<font size=2 face='微软雅黑' color='gray'>最大长度64位，允许中文、英文字母、数字、特殊字符</font>"));

		//把输入内容部分布局添加到总体布局：
		totality_layout->addLayout(newpassword_layout);
	}
	


	//确认按钮部分布局：
	{
		auto hlayout = new QHBoxLayout;
		hlayout->addWidget(m_ok_btn);
		hlayout->addStretch();		//弹簧
		hlayout->addWidget(m_cancel_btn);

		//添加到总体布局：
		totality_layout->addLayout(hlayout);
	}

	//信号槽连接：
	connectinit();
}

void UserDetail_toModifyPasswordDlg::setUserData(const QJsonObject& object)
{
	//获取用户数据：
	m_user = object;
	m_Newuser = object;


	//部署用户数据信息：
	updateUi();
}

void UserDetail_toModifyPasswordDlg::updateUi()
{
	m_user_id_edit->setText(m_user.value("user_id").toString());
	m_user_id_edit->setEnabled(false);															//用户id不能被修改
	m_user_id_edit->setStyleSheet("background-color:white;color:gray");				//设置用户id为灰色
	m_username_edit->setText(m_user.value("username").toString());
	m_username_edit->setEnabled(false);															//用户id不能被修改
	m_username_edit->setStyleSheet("background-color:white;color:gray");				//设置用户id为灰色
	m_oldpassword_edit->setText(m_user.value("password").toString());
	m_oldpassword_edit->setEnabled(false);															//用户id不能被修改
	m_oldpassword_edit->setStyleSheet("background-color:white;color:gray");				//设置用户id为灰色
	m_newpassword_edit->clear();
}

void UserDetail_toModifyPasswordDlg::connectinit()
{
	//确认按钮：
	connect(m_ok_btn, &QPushButton::released, this,&UserDetail_toModifyPasswordDlg::on_OKBtnslot);
	//回车按钮联动：
	connect(m_newpassword_edit, &QLineEdit::returnPressed, this, [=]
		{
			m_ok_btn->click();
		});
	//取消按钮，关闭该窗口：
	connect(m_cancel_btn, &QPushButton::clicked, this, [=]()
		{
			emit windowclose();
			this->close();
		});

}

void UserDetail_toModifyPasswordDlg::on_OKBtnslot()
{
	//如果新密码为空：
	if (m_newpassword_edit->text().isEmpty())
	{
		emit windowclose();
		this->close();
		return;
	}

	//数据修改提示:
	if (QMessageBox::Cancel == QMessageBox::question(0, QObject::tr("tip~~~"), "确定修改该用户的密码么", QMessageBox::Yes | QMessageBox::Cancel))
	{
		emit windowclose();
		this->close();
	}


	//更新数据：
	m_Newuser.insert("password", m_newpassword_edit->text());

	//如果没有数据更新：
	if (m_Newuser == m_user)
	{
		m_Newuser = QJsonObject();

		emit windowclose();
		this->close();
	}


	SHttpClient(QUrl("http://" + sApp->globalConfig()->value("host/host").toString() + QString("/api/user/modify?user_id=%1").arg(m_user.value("user_id").toString())))
		.debug(true)
			.header("Authorization", "Bearer" + sApp->userData("user/token").toString())
				.json({{"password",m_newpassword_edit->text()}})
					.success([=](const QByteArray& data)
						{
							QJsonParseError error;
							auto jdom = QJsonDocument::fromJson(data, &error);
							if (error.error != QJsonParseError::NoError && jdom["code"].toInt() > 1000) {
								return;
							}
							else 
							{
								emit UserDataChanged(m_Newuser);
								this->close();
							}
						})
						.post();


	this->close();
}

